<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue"></script>
    <link rel="stylesheet" href="https://at.alicdn.com/t/font_1061737_z2c7nalzozq.css">
</head>

<body>
    <div id="app">
        <div>
            <div ref="pasteRegion" @mouseleave="mouseOut" @mouseenter="mouseIn" class="paste-region" @paste="pasteImg">
                <i class="iconfont icon-upload"></i>
                <ul class="preview">
                    <li title="点击查看" v-for="(imgBase64,index) in imgBase64List" :key="imgBase64.name">
                        <i @click="remove(index)" title="关闭" class="close">×</i>
                        <img :src="imgBase64.url" />
                    </li>
                </ul>
            </div>
            <p style="color:red" v-if="log">{{log}}</p>
        </div>
    </div>
</body>
<style>
    .paste-region {
        position: relative;
        display: inline-block;
        width: 300px;
        min-height: 150px;
        font-size: 16px;
        border: 2px dashed #999;
        cursor: pointer;
    }

     .icon-upload {
        display: inline-block;
        text-align: center;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        box-sizing: border-box;
        color: #C0C4CC;
        font-size: 6em;
        z-index: -1;
    }

    .icon-upload::after {
        content: "鼠标点击此处ctrl+v";
        display: block;
        color: #409EFF;
        font-size: 20px;
    }

    .preview,
    .preview li {
        margin: 0;
        padding: 0;
    }

    .preview {}

    .preview li {
        display: inline-block;
        position: relative;
        box-sizing: border-box;
        list-style: none;
        box-shadow: 2px 2px 5px #eee;
        width: 60px;
        margin: 5px;
    }

    .preview img {
        width: 100%;
    }

    .preview img:hover {
        box-shadow: 2px 2px 5px #999;
    }

    .close {
        display: inline-block;
        position: absolute;
        width: 20px;
        height: 20px;
        line-height: 16px;
        top: -10px;
        right: -7px;
        border-radius: 10px;
        background: #f6f6f6;
        font-size: 22px;
        color: red;
    }
</style>
<script>
    new Vue({
        el: "#app",

        props: {

        },
        data: () => {
            return {
                isHover: false,
                imgBase64List: [],
                log: "",
            }
        },
        methods: {
            remove(index) {
                this.imgBase64List.splice(index, 1);
            },
            mouseIn() {
                this.isHover = true;
                console.log(this.$refs['pasteRegion']);
            },
            mouseOut() {
                this.isHover = false;
                this.log = false;
            },
            pasteImg(event) {
                let that = this;
                //鼠标位于粘贴处
                if (!this.isHover) {
                    return;
                }

                var items = (event.clipboardData).items;
                var file = null;
                if (items && items.length) {
                    // 搜索剪切板items
                    for (var i = 0; i < items.length; i++) {
                        if (items[i].type.indexOf('image') !== -1) {
                            file = items[i].getAsFile();
                            break;
                        }
                    }
                } else {
                    this.log = "当前浏览器不支持";
                    return;
                }
                if (!file) {
                    this.log = "粘贴内容非图片";
                    return;
                }
                // 此时file就是我们的剪切板中的图片对象
                // 如果需要预览，可以执行下面代码
                var reader = new FileReader()
                reader.onload = function (event) {
                    let base64 = event.target.result;
                    that.imgBase64List.push({
                        url: base64,
                        name: file.name
                    });
                    that.emit(base64);
                }
                reader.readAsDataURL(file);
                // 如果不需要预览，上面这段可以忽略
                event.preventDefault();
            },
            emit(data){
                this.$emit("getSonData",data);
            }
        },
        mounted: () => {
        }
    })
</script>

</html>